<?php
require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'sdk.class.php';
require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'util/oss_util.class.php';

//关于endpoint的介绍见, endpoint就是OSS访问的域名
//http://docs.aliyun.com/?spm=5176.7114037.1996646101.11.XMMlZa&pos=6#/oss/product-documentation/domain-region
class AliyunOss
{
    const endpoint = OSS_ENDPOINT;
//    const accessKeyId = OSS_ACCESS_ID;
//    const accessKeySecret = OSS_ACCESS_KEY;
    const bucket = OSS_TEST_BUCKET;

    private static $accessKeyId = OSS_ACCESS_ID;
    private static $accessKeySecret = OSS_ACCESS_KEY;
    private static $securityToken = null;

    public static function get_oss_client()
    {
        if (OSS_IS_STS)
        {
            self::get_access_token();
        }
        $oss = new ALIOSS(self::$accessKeyId, self::$accessKeySecret, self::endpoint, self::$securityToken);
        return $oss;
    }

    public static function my_echo($msg)
    {
        $new_line = " <br />";
        echo $msg . $new_line;
    }

    public static function get_bucket_name()
    {
        return self::bucket;
    }

    public static function create_bucket()
    {
        $oss = self::get_oss_client();
        $bucket = self::get_bucket_name();
        $acl = ALIOSS::OSS_ACL_TYPE_PUBLIC_READ;
        $res = $oss->create_bucket($bucket, $acl);
//        $msg = "创建bucket " . $bucket;
//        OSSUtil::print_res($res, $msg);
    }

    public static function get_object_url($object, $prefix = '')
    {
        return 'https://' . self::bucket . '.' . self::endpoint . (empty($prefix) ? '' : '/' . $prefix) . '/' . $object;
    }

    private static function get_access_token()
    {
        if (!self::is_credentials_expired())
        {
            $content = file_get_contents(OSS_ACCESS_URL);
            $result = json_decode($content, true);
            $_SESSION['OSS_CREDENTIALS'] = $result['result']['Credentials'];
        }
        self::$accessKeyId = $_SESSION['OSS_CREDENTIALS']['AccessKeyId'];
        self::$accessKeySecret = $_SESSION['OSS_CREDENTIALS']['AccessKeySecret'];
        self::$securityToken = $_SESSION['OSS_CREDENTIALS']['SecurityToken'];
    }

    private static function is_credentials_expired()
    {
        return isset($_SESSION['OSS_CREDENTIALS']['Expiration']) && strtotime($_SESSION['OSS_CREDENTIALS']['Expiration']) >= time();
    }

    static function upload_file_by_file($file_path, $file_name, $options = null)
    {
        //初始化
        $bucket = AliyunOss::get_bucket_name();
        $oss = AliyunOss::get_oss_client();

        AliyunOss::create_bucket();

        $oss->set_enable_domain_style(true);

        try
        {
            $res = $oss->upload_file_by_file($bucket, $file_name, $file_path, $options);
            return 'https://' . $bucket . '.' . AliyunOss::endpoint . '/' . $file_name;
        }
        catch (Exception $ex)
        {
            return false;
        }
    }

    static function batch_upload_file($object, $directory)
    {
        //初始化
        $bucket = AliyunOss::get_bucket_name();
        $oss = AliyunOss::get_oss_client();

        AliyunOss::create_bucket();

        $oss->set_enable_domain_style(true);

        $options = array(
            'bucket' => $bucket,
            'object' => $object,
            'directory' => $directory,
        );
        try
        {
            return $oss->batch_upload_file($options);
        }
        catch (Exception $ex)
        {
            return false;
        }
    }

    static function download_object($object, $localfile)
    {
        //初始化
        $bucket = AliyunOss::get_bucket_name();
        $oss = AliyunOss::get_oss_client();

//        AliyunOss::create_bucket();

        $oss->set_enable_domain_style(true);

        $options = array(
            ALIOSS::OSS_FILE_DOWNLOAD => $localfile,
            ALIOSS::OSS_QUERY_STRING => array(
                'x-oss-process' => 'image/resize,w_300,h_300/watermark,type_d3F5LXplbmhlaQ,size_30,text_SGVsbG8g5Zu-54mH5pyN5YqhIQ',
            ),
        );
        $res = $oss->get_object($bucket, $object, $options);
//        if (file_exists($localfile))
//        {
//            unlink($localfile);
//        }
    }
}